package cn.com.open.itembank;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.WebMvcRegistrationsAdapter;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.Method;
import java.util.Set;

@RestController
@SpringBootApplication
@ServletComponentScan
@EnableCaching
@EnableTransactionManagement
@RefreshScope
@EnableAsync
@EnableDiscoveryClient
@EnableFeignClients
public class ItemApiApplication {

	@Value("${base.request.url}")
	private String baseRequestUrl;

	@Value("${app.name}")
	private String name;

	@RequestMapping(value = "/version", method = RequestMethod.GET)
	public String version() {
		return name;
	}

	@RequestMapping(value = "/dnotdelet/mom.html", method = RequestMethod.GET)
	public String home() {
		return "helloworld!";
	}

	public static void main(String[] args) {
		SpringApplication.run(ItemApiApplication.class, args);
	}

	@Bean
	public WebMvcRegistrationsAdapter webMvcRegistrationsHandlerMapping() {
		return new WebMvcRegistrationsAdapter() {
			@Override
			public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
				return new RequestMappingHandlerMapping() {
					private final static String EXCLUDE_PATH = "/dnotdelet/mom.html";
					@Override
					protected void registerHandlerMethod(Object handler, Method method, RequestMappingInfo mapping) {
						Set<String> patterns = mapping.getPatternsCondition().getPatterns();
						boolean flag = patterns.stream().anyMatch(e -> e.equals(EXCLUDE_PATH));
						if (!flag) {
							Class<?> beanType = method.getDeclaringClass();
							if (AnnotationUtils.findAnnotation(beanType, RestController.class) != null) {
								PatternsRequestCondition apiPattern = new PatternsRequestCondition(baseRequestUrl).combine(mapping.getPatternsCondition());
								mapping = new RequestMappingInfo(mapping.getName(), apiPattern,
										mapping.getMethodsCondition(), mapping.getParamsCondition(),
										mapping.getHeadersCondition(), mapping.getConsumesCondition(),
										mapping.getProducesCondition(), mapping.getCustomCondition());
							}
						}
						super.registerHandlerMethod(handler, method, mapping);
					}
				};
			}
		};
	}
}
